home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / calc202a.lha / calc-2.02a / calc-embed.el (.txt) < prev    next >
LaTeX Document  |  1993-06-01  |  42KB  |  1,117 lines

  1. ;; Calculator for GNU Emacs, part II [calc-embed.el]
  2. ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3. ;; Written by Dave Gillespie, daveg@synaptics.com.
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is distributed in the hope that it will be useful,
  6. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  7. ;; accepts responsibility to anyone for the consequences of using it
  8. ;; or for whether it serves any particular purpose or works at all,
  9. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  10. ;; License for full details.
  11. ;; Everyone is granted permission to copy, modify and redistribute
  12. ;; GNU Emacs, but only under the conditions described in the
  13. ;; GNU Emacs General Public License.   A copy of this license is
  14. ;; supposed to have been given to you along with GNU Emacs so you
  15. ;; can know your rights and responsibilities.  It should be in a
  16. ;; file named COPYING.  Among other things, the copyright notice
  17. ;; and this notice must be preserved on all copies.
  18. ;; This file is autoloaded from calc-ext.el.
  19. (require 'calc-ext)
  20. (require 'calc-macs)
  21. (defun calc-Need-calc-embed () nil)
  22. (defun calc-show-plain (n)
  23.   (interactive "P")
  24.   (calc-wrapper
  25.    (calc-set-command-flag 'renum-stack)
  26.    (message (if (calc-change-mode 'calc-show-plain n nil t)
  27.         "Including \"plain\" formulas in Calc Embedded mode."
  28.           "Omitting \"plain\" formulas in Calc Embedded mode.")))
  29. ;;; Things to do for Embedded Mode:
  30. ;;;  Detect and strip off unexpected labels during reading.
  31. ;;;  Get calc-grab-region to use math-read-big-expr.
  32. ;;;  If calc-show-plain, main body should have only righthand side of => expr.
  33. ;;;  Handle tabs that have crept into embedded formulas.
  34. ;;;  After "switching to new formula", home cursor to that formula.
  35. ;;;  Do something like \evalto ... \to for \gets operators.
  36. (defvar calc-embedded-modes nil)
  37. (defvar calc-embedded-globals nil)
  38. (defvar calc-embedded-active nil)
  39. (make-variable-buffer-local 'calc-embedded-all-active)
  40. (make-variable-buffer-local 'calc-embedded-some-active)
  41. (defvar calc-embedded-open-formula "\\`\\|^\n\\|\\$\\$?\\|\\\\\\[\\|^\\\\begin.*\n\\|^@.*\n\\|^\\.EQ.*\n\\|\\\\(\\|^%\n\\|^\\.\\\\\"\n"
  42.   "*A regular expression for the opening delimiter of a formula used by
  43. calc-embedded.")
  44. (defvar calc-embedded-close-formula "\\'\\|\n$\\|\\$\\$?\\|\\\\]\\|^\\\\end.*\n\\|^@.*\n\\|^\\.EN.*\n\\|\\\\)\\|\n%\n\\|^\\.\\\\\"\n"
  45.   "*A regular expression for the closing delimiter of a formula used by
  46. calc-embedded.")
  47. (defvar calc-embedded-open-word "^\\|[^-+0-9.eE]"
  48.   "*A regular expression for the opening delimiter of a formula used by
  49. calc-embedded-word.")
  50. (defvar calc-embedded-close-word "$\\|[^-+0-9.eE]"
  51.   "*A regular expression for the closing delimiter of a formula used by
  52. calc-embedded-word.")
  53. (defvar calc-embedded-open-plain "%%% "
  54.   "*A string which is the opening delimiter for a \"plain\" formula.
  55. If calc-show-plain mode is enabled, this is inserted at the front of
  56. each formula.")
  57. (defvar calc-embedded-close-plain " %%%\n"
  58.   "*A string which is the closing delimiter for a \"plain\" formula.
  59. See calc-embedded-open-plain.")
  60. (defvar calc-embedded-open-new-formula "\n\n"
  61.   "*A string which is inserted at front of formula by calc-embedded-new-formula.")
  62. (defvar calc-embedded-close-new-formula "\n\n"
  63.   "*A string which is inserted at end of formula by calc-embedded-new-formula.")
  64. (defvar calc-embedded-announce-formula "%Embed\n\\(% .*\n\\)*"
  65.   "*A regular expression which is sure to be followed by a calc-embedded formula." )
  66. (defvar calc-embedded-open-mode "% "
  67.   "*A string which should precede calc-embedded mode annotations.
  68. This is not required to be present for user-written mode annotations.")
  69. (defvar calc-embedded-close-mode "\n"
  70.   "*A string which should follow calc-embedded mode annotations.
  71. This is not required to be present for user-written mode annotations.")
  72. (defconst calc-embedded-mode-vars '(("precision" . calc-internal-prec)
  73.                     ("word-size" . calc-word-size)
  74.                     ("angles" . calc-angle-mode)
  75.                     ("symbolic" . calc-symbolic-mode)
  76.                     ("matrix" . calc-matrix-mode)
  77.                     ("fractions" . calc-prefer-frac)
  78.                     ("complex" . calc-complex-mode)
  79.                     ("simplify" . calc-simplify-mode)
  80.                     ("language" . the-language)
  81.                     ("plain" . calc-show-plain)
  82.                     ("break" . calc-line-breaking)
  83.                     ("justify" . the-display-just)
  84.                     ("left-label" . calc-left-label)
  85.                     ("right-label" . calc-right-label)
  86.                     ("radix" . calc-number-radix)
  87.                     ("leading-zeros" . calc-leading-zeros)
  88.                     ("grouping" . calc-group-digits)
  89.                     ("group-char" . calc-group-char)
  90.                     ("point-char" . calc-point-char)
  91.                     ("frac-format" . calc-frac-format)
  92.                     ("float-format" . calc-float-format)
  93.                     ("complex-format" . calc-complex-format)
  94.                     ("hms-format" . calc-hms-format)
  95.                     ("date-format" . calc-date-format)
  96.                     ("matrix-justify" . calc-matrix-just)
  97.                     ("full-vectors" . calc-full-vectors)
  98.                     ("break-vectors" . calc-break-vectors)
  99.                     ("vector-commas" . calc-vector-commas)
  100.                     ("vector-brackets" . calc-vector-brackets)
  101.                     ("matrix-brackets" . calc-matrix-brackets)
  102.                     ("strings" . calc-display-strings)
  103. ;;; Format of calc-embedded-info vector:
  104. ;;;    0   Editing buffer.
  105. ;;;    1   Calculator buffer.
  106. ;;;    2   Top of current formula (marker).
  107. ;;;    3   Bottom of current formula (marker).
  108. ;;;    4   Top of current formula's delimiters (marker).
  109. ;;;    5   Bottom of current formula's delimiters (marker).
  110. ;;;    6   String representation of current formula.
  111. ;;;    7   Non-nil if formula is embedded within a single line.
  112. ;;;    8   Internal representation of current formula.
  113. ;;;    9   Variable assigned by this formula, or nil.
  114. ;;;   10   List of variables upon which this formula depends.
  115. ;;;   11   Evaluated value of the formula, or nil.
  116. ;;;   12   Mode settings for current formula.
  117. ;;;   13   Local mode settings for current formula.
  118. ;;;   14   Permanent mode settings for current formula.
  119. ;;;   15   Global mode settings for editing buffer.
  120. ;;; calc-embedded-active is an a-list keyed on buffers; each cdr is a
  121. ;;; sorted list of calc-embedded-infos in that buffer.  We do this
  122. ;;; rather than using buffer-local variables because the latter are
  123. ;;; thrown away when a buffer changes major modes.
  124. (defun calc-do-embedded (arg end obeg oend)
  125.   (if calc-embedded-info
  126.       ;; Turn embedded mode off or switch to a new buffer.
  127.       (cond ((eq (current-buffer) (aref calc-embedded-info 1))
  128.          (let ((calcbuf (current-buffer))
  129.            (buf (aref calc-embedded-info 0)))
  130.            (calc-embedded-original-buffer t)
  131.            (calc-embedded nil)
  132.            (switch-to-buffer calcbuf)))
  133.         ((eq (current-buffer) (aref calc-embedded-info 0))
  134.          (let* ((info calc-embedded-info)
  135.             (mode calc-embedded-modes))
  136.            (save-excursion
  137.          (set-buffer (aref info 1))
  138.          (if (and (> (calc-stack-size) 0)
  139.               (equal (calc-top 1 'full) (aref info 8)))
  140.              (let ((calc-no-refresh-evaltos t))
  141.                (if (calc-top 1 'sel)
  142.                (calc-unselect 1))
  143.                (calc-embedded-set-modes
  144.             (aref info 15) (aref info 12) (aref info 14))
  145.                (let ((calc-embedded-info nil))
  146.              (calc-wrapper (calc-pop-stack))))
  147.            (calc-set-mode-line)))
  148.            (setq calc-embedded-info nil
  149.              mode-line-buffer-identification (car mode)
  150.              truncate-lines (nth 2 mode)
  151.              buffer-read-only nil)
  152.            (use-local-map (nth 1 mode))
  153.            (set-buffer-modified-p (buffer-modified-p))
  154.            (or calc-embedded-quiet
  155.            (message "Back to %s mode." mode-name))))
  156.         (t
  157.          (if (buffer-name (aref calc-embedded-info 0))
  158.          (save-excursion
  159.            (set-buffer (aref calc-embedded-info 0))
  160.            (or (y-or-n-p "Cancel Calc Embedded mode in buffer %s? "
  161.                  (buffer-name))
  162.                (keyboard-quit))
  163.            (calc-embedded nil)))
  164.          (calc-embedded arg end obeg oend)))
  165.     ;; Turn embedded mode on.
  166.     (calc-plain-buffer-only)
  167.     (let ((modes (list mode-line-buffer-identification
  168.                (current-local-map)
  169.                truncate-lines))
  170.       top bot outer-top outer-bot
  171.       info chg ident)
  172.       (barf-if-buffer-read-only)
  173.       (or calc-embedded-globals
  174.       (calc-find-globals))
  175.       (setq info (calc-embedded-make-info (point) nil t arg end obeg oend))
  176.       (if (eq (car-safe (aref info 8)) 'error)
  177.       (progn
  178.         (goto-char (nth 1 (aref info 8)))
  179.         (error (nth 2 (aref info 8)))))
  180.       (let ((mode-line-buffer-identification mode-line-buffer-identification)
  181.         (calc-embedded-info info)
  182.         (calc-embedded-no-reselect t))
  183.     (calc-wrapper
  184.      (let* ((okay nil)
  185.         (calc-no-refresh-evaltos t))
  186.        (setq chg (calc-embedded-set-modes
  187.               (aref info 15) (aref info 12) (aref info 13)))
  188.        (if (aref info 8)
  189.            (calc-push (calc-normalize (aref info 8)))
  190.          (calc-alg-entry)))
  191.      (setq calc-undo-list nil
  192.            calc-redo-list nil
  193.            ident mode-line-buffer-identification)))
  194.       (setq calc-embedded-info info
  195.         calc-embedded-modes modes
  196.         mode-line-buffer-identification ident
  197.         truncate-lines t
  198.         buffer-read-only t)
  199.       (set-buffer-modified-p (buffer-modified-p))
  200.       (use-local-map calc-mode-map)
  201.       (setq calc-no-refresh-evaltos nil)
  202.       (and chg calc-any-evaltos (calc-wrapper (calc-refresh-evaltos)))
  203.       (or (eq calc-embedded-quiet t)
  204.       (message "Embedded Calc mode enabled.  %s to return to normal."
  205.            (if calc-embedded-quiet
  206.                "Type `M-# x'"
  207.              "Give this command again")))))
  208.   (scroll-down 0)    ; fix a bug which occurs when truncate-lines is changed.
  209. (setq calc-embedded-quiet nil)
  210. (defun calc-embedded-select (arg)
  211.   (interactive "P")
  212.   (calc-embedded arg)
  213.   (and calc-embedded-info
  214.        (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-evalto)
  215.        (calc-select-part 1))
  216.   (and calc-embedded-info
  217.        (or (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-assign)
  218.        (and (eq (car-safe (aref calc-embedded-info 8)) 'calcFunc-evalto)
  219.         (eq (car-safe (nth 1 (aref calc-embedded-info 8)))
  220.             'calcFunc-assign)))
  221.        (calc-select-part 2))
  222. (defun calc-embedded-update-formula (arg)
  223.   (interactive "P")
  224.   (if arg
  225.       (let ((entry (assq (current-buffer) calc-embedded-active)))
  226.     (while (setq entry (cdr entry))
  227.       (and (eq (car-safe (aref (car entry) 8)) 'calcFunc-evalto)
  228.            (or (not (consp arg))
  229.            (and (<= (aref (car entry) 2) (region-beginning))
  230.             (>= (aref (car entry) 3) (region-end))))
  231.            (save-excursion
  232.          (calc-embedded-update (car entry) 14 t t)))))
  233.     (if (and calc-embedded-info
  234.          (eq (current-buffer) (aref calc-embedded-info 0))
  235.          (>= (point) (aref calc-embedded-info 4))
  236.          (<= (point) (aref calc-embedded-info 5)))
  237.     (calc-evaluate 1)
  238.       (let* ((opt (point))
  239.          (info (calc-embedded-make-info (point) nil t))
  240.          (pt (- opt (aref info 4))))
  241.     (or (eq (car-safe (aref info 8)) 'error)
  242.         (progn
  243.           (save-excursion
  244.         (calc-embedded-update info 14 'eval t))
  245.           (goto-char (+ (aref info 4) pt)))))))
  246. (defun calc-embedded-edit (arg)
  247.   (interactive "P")
  248.   (let ((info (calc-embedded-make-info (point) nil t arg))
  249.     str)
  250.     (if (eq (car-safe (aref info 8)) 'error)
  251.     (progn
  252.       (goto-char (nth 1 (aref info 8)))
  253.       (error (nth 2 (aref info 8)))))
  254.     (calc-wrapper
  255.      (setq str (math-showing-full-precision
  256.         (math-format-nice-expr (aref info 8) (screen-width))))
  257.      (calc-edit-mode (list 'calc-embedded-finish-edit info))
  258.      (insert str "\n")))
  259.   (calc-show-edit-buffer)
  260. (defun calc-embedded-finish-edit (info)
  261.   (let ((buf (current-buffer))
  262.     (str (buffer-substring (point) (point-max)))
  263.     (start (point))
  264.     pos)
  265.     (switch-to-buffer calc-original-buffer)
  266.     (let ((val (save-excursion
  267.          (set-buffer (aref info 1))
  268.          (let ((calc-language nil)
  269.                (math-expr-opers math-standard-opers))
  270.            (math-read-expr str)))))
  271.       (if (eq (car-safe val) 'error)
  272.       (progn
  273.         (switch-to-buffer buf)
  274.         (goto-char (+ start (nth 1 val)))
  275.         (error (nth 2 val))))
  276.       (calc-embedded-original-buffer t info)
  277.       (aset info 8 val)
  278.       (calc-embedded-update info 14 t t)))
  279. (defun calc-do-embedded-activate (arg cbuf)
  280.   (calc-plain-buffer-only)
  281.   (if arg
  282.       (calc-embedded-forget))
  283.   (calc-find-globals)
  284.   (if (< (prefix-numeric-value arg) 0)
  285.       (message "Deactivating %s for Calc Embedded mode." (buffer-name))
  286.     (message "Activating %s for Calc Embedded mode..." (buffer-name))
  287.     (save-excursion
  288.       (let* ((active (assq (current-buffer) calc-embedded-active))
  289.          (info active)
  290.          (pat " := \\| \\\\gets \\| => \\| \\\\evalto "))
  291.     (if calc-embedded-announce-formula
  292.         (setq pat (format "%s\\|\\(%s\\)"
  293.                   pat calc-embedded-announce-formula)))
  294.     (while (setq info (cdr info))
  295.       (or (equal (buffer-substring (aref (car info) 2) (aref (car info) 3))
  296.              (aref (car info) 6))
  297.           (setcdr active (delq (car info) (cdr active)))))
  298.     (goto-char (point-min))
  299.     (while (re-search-forward pat nil t)
  300.       (if (looking-at calc-embedded-open-formula)
  301.           (goto-char (match-end 1)))
  302.       (setq info (calc-embedded-make-info (point) cbuf nil))
  303.       (or (eq (car-safe (aref info 8)) 'error)
  304.           (goto-char (aref info 5))))))
  305.     (message "Activating %s for Calc Embedded mode...done" (buffer-name)))
  306.   (calc-embedded-active-state t)
  307. (defun calc-plain-buffer-only ()
  308.   (if (memq major-mode '(calc-mode calc-trail-mode calc-edit-mode))
  309.       (error "This command should be used in a normal editing buffer"))
  310. (defun calc-embedded-active-state (state)
  311.   (or (assq 'calc-embedded-all-active minor-mode-alist)
  312.       (setq minor-mode-alist
  313.         (cons '(calc-embedded-all-active " Active")
  314.           (cons '(calc-embedded-some-active " ~Active")
  315.             minor-mode-alist))))
  316.   (let ((active (assq (current-buffer) calc-embedded-active)))
  317.     (or (cdr active)
  318.     (setq state nil)))
  319.   (and (eq state 'more) calc-embedded-all-active (setq state t))
  320.   (setq calc-embedded-all-active (eq state t)
  321.     calc-embedded-some-active (not (memq state '(nil t))))
  322.   (set-buffer-modified-p (buffer-modified-p))
  323. (defun calc-embedded-original-buffer (switch &optional info)
  324.   (or info (setq info calc-embedded-info))
  325.   (or (buffer-name (aref info 0))
  326.       (progn
  327.     (error "Calc embedded mode: Original buffer has been killed")))
  328.   (if switch
  329.       (set-buffer (aref info 0)))
  330. (defun calc-embedded-word ()
  331.   (interactive)
  332.   (calc-embedded '(4))
  333. (defun calc-embedded-mark-formula (&optional body-only)
  334.   "Put point at the beginning of this Calc formula, mark at the end.
  335. This normally marks the whole formula, including surrounding delimiters.
  336. With any prefix argument, marks only the formula itself."
  337.   (interactive "P")
  338.   (and (eq major-mode 'calc-mode)
  339.        (error "This command should be used in a normal editing buffer"))
  340.   (let (top bot outer-top outer-bot)
  341.     (save-excursion
  342.       (calc-embedded-find-bounds body-only))
  343.     (push-mark (if body-only bot outer-bot) t)
  344.     (goto-char (if body-only top outer-top)))
  345. (defun calc-embedded-find-bounds (&optional plain)
  346.   ;; (while (and (bolp) (eq (following-char) ?\n))
  347.   ;;  (forward-char 1))
  348.   (and (eolp) (bolp) (not (eq (char-after (- (point) 2)) ?\n))
  349.        (forward-char -1))
  350.   (let ((home (point)))
  351.     (or (and (looking-at calc-embedded-open-formula)
  352.          (not (looking-at calc-embedded-close-formula)))
  353.     (re-search-backward calc-embedded-open-formula nil t)
  354.     (error "Can't find start of formula"))
  355.     (and (eq (preceding-char) ?\$)  ; backward search for \$\$? won't back
  356.      (eq (following-char) ?\$)  ; up over a second $, so do it by hand.
  357.      (forward-char -1))
  358.     (setq outer-top (point))
  359.     (goto-char (match-end 0))
  360.     (if (eq (following-char) ?\n)
  361.     (forward-char 1))
  362.     (or (bolp)
  363.     (while (eq (following-char) ?\ )
  364.       (forward-char 1)))
  365.     (or (eq plain 'plain)
  366.     (if (looking-at (regexp-quote calc-embedded-open-plain))
  367.         (progn
  368.           (goto-char (match-end 0))
  369.           (search-forward calc-embedded-close-plain))))
  370.     (setq top (point))
  371.     (or (re-search-forward calc-embedded-close-formula nil t)
  372.     (error "Can't find end of formula"))
  373.     (if (< (point) home)
  374.     (error "Not inside a formula"))
  375.     (and (eq (following-char) ?\n) (not (bolp))
  376.      (forward-char 1))
  377.     (setq outer-bot (point))
  378.     (goto-char (match-beginning 0))
  379.     (if (eq (preceding-char) ?\n)
  380.     (backward-char 1))
  381.     (or (eolp)
  382.     (while (eq (preceding-char) ?\ )
  383.       (backward-char 1)))
  384.     (setq bot (point)))
  385. (defun calc-embedded-kill-formula ()
  386.   "Kill the formula surrounding point.
  387. If Calc Embedded mode was active, this deactivates it.
  388. The formula (including its surrounding delimiters) is saved in the kill ring.
  389. The command \\[yank] can retrieve it from there."
  390.   (interactive)
  391.   (and calc-embedded-info
  392.        (calc-embedded nil))
  393.   (calc-embedded-mark-formula)
  394.   (kill-region (point) (mark))
  395.   (pop-mark)
  396. (defun calc-embedded-copy-formula-as-kill ()
  397.   "Save the formula surrounding point as if killed, but don't kill it."
  398.   (interactive)
  399.   (save-excursion
  400.     (calc-embedded-mark-formula)
  401.     (copy-region-as-kill (point) (mark))
  402.     (pop-mark))
  403. (defun calc-embedded-duplicate ()
  404.   (interactive)
  405.   (let ((already calc-embedded-info)
  406.     top bot outer-top outer-bot new-top)
  407.     (if calc-embedded-info
  408.     (progn
  409.       (setq top (+ (aref calc-embedded-info 2))
  410.         bot (+ (aref calc-embedded-info 3))
  411.         outer-top (+ (aref calc-embedded-info 4))
  412.         outer-bot (+ (aref calc-embedded-info 5)))
  413.       (calc-embedded nil))
  414.       (calc-embedded-find-bounds))
  415.     (goto-char outer-bot)
  416.     (insert "\n")
  417.     (setq new-top (point))
  418.     (insert-buffer-substring (current-buffer) outer-top outer-bot)
  419.     (goto-char (+ new-top (- top outer-top)))
  420.     (let ((calc-embedded-quiet (if already t 'x)))
  421.       (calc-embedded (+ new-top (- top outer-top))
  422.              (+ new-top (- bot outer-top))
  423.              new-top
  424.              (+ new-top (- outer-bot outer-top)))))
  425. (defun calc-embedded-next (arg)
  426.   (interactive "P")
  427.   (setq arg (prefix-numeric-value arg))
  428.   (let* ((active (cdr (assq (current-buffer) calc-embedded-active)))
  429.      (p active)
  430.      (num (length active)))
  431.     (or active
  432.     (error "No active formulas in buffer"))
  433.     (cond ((= arg 0))
  434.       ((= arg -1)
  435.        (if (<= (point) (aref (car active) 3))
  436.            (goto-char (aref (nth (1- num) active) 2))
  437.          (while (and (cdr p)
  438.              (> (point) (aref (nth 1 p) 3)))
  439.            (setq p (cdr p)))
  440.          (goto-char (aref (car p) 2))))
  441.       ((< arg -1)
  442.        (calc-embedded-next -1)
  443.        (calc-embedded-next (+ (* num 1000) arg 1)))
  444.       (t
  445.        (setq arg (1+ (% (1- arg) num)))
  446.        (while (and p (>= (point) (aref (car p) 2)))
  447.          (setq p (cdr p)))
  448.        (while (> (setq arg (1- arg)) 0)
  449.          (setq p (if p (cdr p) (cdr active))))
  450.        (goto-char (aref (car (or p active)) 2)))))
  451. (defun calc-embedded-previous (arg)
  452.   (interactive "p")
  453.   (calc-embedded-next (- (prefix-numeric-value arg)))
  454. (defun calc-embedded-new-formula ()
  455.   (interactive)
  456.   (and (eq major-mode 'calc-mode)
  457.        (error "This command should be used in a normal editing buffer"))
  458.   (if calc-embedded-info
  459.       (calc-embedded nil))
  460.   (let (top bot outer-top outer-bot)
  461.     (if (and (eq (preceding-char) ?\n)
  462.          (string-match "\\`\n" calc-embedded-open-new-formula))
  463.     (progn
  464.       (setq outer-top (1- (point)))
  465.       (forward-char -1)
  466.       (insert (substring calc-embedded-open-new-formula 1)))
  467.       (setq outer-top (point))
  468.       (insert calc-embedded-open-new-formula))
  469.     (setq top (point))
  470.     (insert " ")
  471.     (setq bot (point))
  472.     (insert calc-embedded-close-new-formula)
  473.     (if (and (eq (following-char) ?\n)
  474.          (string-match "\n\\'" calc-embedded-close-new-formula))
  475.     (delete-char 1))
  476.     (setq outer-bot (point))
  477.     (goto-char top)
  478.     (let ((calc-embedded-quiet 'x))
  479.       (calc-embedded top bot outer-top outer-bot)))
  480. (defun calc-embedded-forget ()
  481.   (interactive)
  482.   (setq calc-embedded-active (delq (assq (current-buffer) calc-embedded-active)
  483.                    calc-embedded-active))
  484.   (calc-embedded-active-state nil)
  485. (defun calc-embedded-set-modes (gmodes modes local-modes &optional temp)
  486.   (let ((the-language (calc-embedded-language))
  487.     (the-display-just (calc-embedded-justify))
  488.     (v gmodes)
  489.     (changed nil)
  490.     found value)
  491.     (while v
  492.       (or (symbolp (car v))
  493.       (and (setq found (assq (car (car v)) modes))
  494.            (not (eq (cdr found) 'default)))
  495.       (and (setq found (assq (car (car v)) local-modes))
  496.            (not (eq (cdr found) 'default)))
  497.       (progn
  498.         (if (eq (setq value (cdr (car v))) 'default)
  499.         (setq value (cdr (assq (car (car v)) calc-mode-var-list))))
  500.         (equal (symbol-value (car (car v))) value))
  501.       (progn
  502.         (setq changed t)
  503.         (if temp (setq prev-modes (cons (cons (car (car v))
  504.                           (symbol-value (car (car v))))
  505.                         prev-modes)))
  506.         (set (car (car v)) value)))
  507.       (setq v (cdr v)))
  508.     (setq v modes)
  509.     (while v
  510.       (or (and (setq found (assq (car (car v)) local-modes))
  511.            (not (eq (cdr found) 'default)))
  512.       (eq (setq value (cdr (car v))) 'default)
  513.       (equal (symbol-value (car (car v))) value)
  514.       (progn
  515.         (setq changed t)
  516.         (if temp (setq prev-modes (cons (cons (car (car v))
  517.                           (symbol-value (car (car v))))
  518.                         prev-modes)))
  519.         (set (car (car v)) value)))
  520.       (setq v (cdr v)))
  521.     (setq v local-modes)
  522.     (while v
  523.       (or (eq (setq value (cdr (car v))) 'default)
  524.       (equal (symbol-value (car (car v))) value)
  525.       (progn
  526.         (setq changed t)
  527.         (if temp (setq prev-modes (cons (cons (car (car v))
  528.                           (symbol-value (car (car v))))
  529.                         prev-modes)))
  530.         (set (car (car v)) value)))
  531.       (setq v (cdr v)))
  532.     (and changed (not (eq temp t))
  533.      (progn
  534.        (calc-embedded-set-justify the-display-just)
  535.        (calc-embedded-set-language the-language)))
  536.     (and changed (not temp)
  537.      (progn
  538.        (setq calc-full-float-format (list (if (eq (car calc-float-format)
  539.                               'fix)
  540.                           'float
  541.                         (car calc-float-format))
  542.                           0))
  543.        (calc-refresh)))
  544.     changed)
  545. (defun calc-embedded-language ()
  546.   (if calc-language-option
  547.       (list calc-language calc-language-option)
  548.     calc-language)
  549. (defun calc-embedded-set-language (lang)
  550.   (let ((option nil))
  551.     (if (consp lang)
  552.     (setq option (nth 1 lang)
  553.           lang (car lang)))
  554.     (or (and (eq lang calc-language)
  555.          (equal option calc-language-option))
  556.     (calc-set-language lang option t)))
  557. (defun calc-embedded-justify ()
  558.   (if calc-display-origin
  559.       (list calc-display-just calc-display-origin)
  560.     calc-display-just)
  561. (defun calc-embedded-set-justify (just)
  562.   (if (consp just)
  563.       (setq calc-display-origin (nth 1 just)
  564.         calc-display-just (car just))
  565.     (setq calc-display-just just
  566.       calc-display-origin nil))
  567. (defun calc-find-globals ()
  568.   (interactive)
  569.   (and (eq major-mode 'calc-mode)
  570.        (error "This command should be used in a normal editing buffer"))
  571.   (make-local-variable 'calc-embedded-globals)
  572.   (let ((case-fold-search nil)
  573.     (modes nil)
  574.     (save-pt (point))
  575.     found value)
  576.     (goto-char (point-min))
  577.     (while (re-search-forward "\\[calc-global-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)\\]" nil t)
  578.       (and (setq found (assoc (buffer-substring (match-beginning 1)
  579.                         (match-end 1))
  580.                   calc-embedded-mode-vars))
  581.        (or (assq (cdr found) modes)
  582.            (setq modes (cons (cons (cdr found)
  583.                        (car (read-from-string
  584.                          (buffer-substring
  585.                           (match-beginning 2)
  586.                           (match-end 2)))))
  587.                  modes)))))
  588.     (setq calc-embedded-globals (cons t modes))
  589.     (goto-char save-pt))
  590. (defun calc-embedded-find-modes ()
  591.   (let ((case-fold-search nil)
  592.     (save-pt (point))
  593.     (no-defaults t)
  594.     (modes nil)
  595.     (emodes nil)
  596.     (pmodes nil)
  597.     found value)
  598.     (while (and no-defaults (search-backward "[calc-" nil t))
  599.       (forward-char 6)
  600.       (or (and (looking-at "mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
  601.            (setq found (assoc (buffer-substring (match-beginning 1)
  602.                             (match-end 1))
  603.                   calc-embedded-mode-vars))
  604.            (or (assq (cdr found) modes)
  605.            (setq modes (cons (cons (cdr found)
  606.                        (car (read-from-string
  607.                          (buffer-substring
  608.                           (match-beginning 2)
  609.                           (match-end 2)))))
  610.                      modes))))
  611.       (and (looking-at "perm-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
  612.            (setq found (assoc (buffer-substring (match-beginning 1)
  613.                             (match-end 1))
  614.                   calc-embedded-mode-vars))
  615.            (or (assq (cdr found) pmodes)
  616.            (setq pmodes (cons (cons (cdr found)
  617.                         (car (read-from-string
  618.                           (buffer-substring
  619.                            (match-beginning 2)
  620.                            (match-end 2)))))
  621.                       pmodes))))
  622.       (and (looking-at "edit-mode: *\\([-a-z]+\\): *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]")
  623.            (setq found (assoc (buffer-substring (match-beginning 1)
  624.                             (match-end 1))
  625.                   calc-embedded-mode-vars))
  626.            (or (assq (cdr found) emodes)
  627.            (setq emodes (cons (cons (cdr found)
  628.                         (car (read-from-string
  629.                           (buffer-substring
  630.                            (match-beginning 2)
  631.                            (match-end 2)))))
  632.                       emodes))))
  633.       (and (looking-at "defaults]")
  634.            (setq no-defaults nil)))
  635.       (backward-char 6))
  636.     (goto-char save-pt)
  637.     (list modes emodes pmodes))
  638. (defun calc-embedded-make-info (point cbuf fresh &optional
  639.                       top bot outer-top outer-bot)
  640.   (let* ((bufentry (assq (current-buffer) calc-embedded-active))
  641.      (found bufentry)
  642.      (force (and fresh top))
  643.      (fixed top)
  644.      (new-info nil)
  645.      info str)
  646.     (or found
  647.     (setq found (list (current-buffer))
  648.           calc-embedded-active (cons found calc-embedded-active)))
  649.     (while (and (cdr found)
  650.         (> point (aref (car (cdr found)) 3)))
  651.       (setq found (cdr found)))
  652.     (if (and (cdr found)
  653.          (>= point (aref (nth 1 found) 2)))
  654.     (setq info (nth 1 found))
  655.       (setq info (make-vector 16 nil)
  656.         new-info t
  657.         fresh t)
  658.       (aset info 0 (current-buffer))
  659.       (aset info 1 (or cbuf (save-excursion
  660.                   (calc-create-buffer)
  661.                   (current-buffer)))))
  662.     (if (and (integerp top) (not bot))  ; started with a user-supplied argument
  663.     (progn
  664.       (if (= (setq arg (prefix-numeric-value arg)) 0)
  665.           (progn
  666.         (aset info 2 (copy-marker (region-beginning)))
  667.         (aset info 3 (copy-marker (region-end))))
  668.         (aset info (if (> arg 0) 2 3) (point-marker))
  669.         (forward-line arg)
  670.         (aset info (if (> arg 0) 3 2) (point-marker)))
  671.       (aset info 4 (copy-marker (aref info 2)))
  672.       (aset info 5 (copy-marker (aref info 3))))
  673.       (if (aref info 4)
  674.       (setq top (aref info 2)
  675.         fixed top)
  676.     (if (consp top)
  677.         (let ((calc-embedded-open-formula calc-embedded-open-word)
  678.           (calc-embedded-close-formula calc-embedded-close-word))
  679.           (calc-embedded-find-bounds 'plain))
  680.       (or top
  681.           (calc-embedded-find-bounds 'plain)))
  682.     (aset info 2 (copy-marker (min top bot)))
  683.     (aset info 3 (copy-marker (max top bot)))
  684.     (aset info 4 (copy-marker (or outer-top (aref info 2))))
  685.     (aset info 5 (copy-marker (or outer-bot (aref info 3))))))
  686.     (goto-char (aref info 2))
  687.     (if new-info
  688.     (progn
  689.       (or (bolp) (aset info 7 t))
  690.       (goto-char (aref info 3))
  691.       (or (bolp) (eolp) (aset info 7 t))))
  692.     (if fresh
  693.     (let ((modes (calc-embedded-find-modes)))
  694.       (aset info 12 (car modes))
  695.       (aset info 13 (nth 1 modes))
  696.       (aset info 14 (nth 2 modes))))
  697.     (aset info 15 calc-embedded-globals)
  698.     (setq str (buffer-substring (aref info 2) (aref info 3)))
  699.     (if (or force
  700.         (not (equal str (aref info 6))))
  701.     (if (and fixed (aref info 6))
  702.         (progn
  703.           (aset info 4 nil)
  704.           (calc-embedded-make-info point cbuf nil)
  705.           (setq new-info nil))
  706.       (let* ((open-plain calc-embedded-open-plain)
  707.          (close-plain calc-embedded-close-plain)
  708.          (pref-len (length open-plain))
  709.          (vars-used nil)
  710.          suff-pos val temp)
  711.         (save-excursion
  712.           (set-buffer (aref info 1))
  713.           (calc-embedded-set-modes (aref info 15)
  714.                        (aref info 12) (aref info 14))
  715.           (if (and (> (length str) pref-len)
  716.                (equal (substring str 0 pref-len) open-plain)
  717.                (setq suff-pos (string-match (regexp-quote close-plain)
  718.                             str pref-len)))
  719.           (setq val (math-read-plain-expr
  720.                  (substring str pref-len suff-pos)))
  721.         (if (string-match "[^ \t\n]" str)
  722.             (setq pref-len 0
  723.               val (math-read-big-expr str))
  724.           (setq val nil))))
  725.         (if (eq (car-safe val) 'error)
  726.         (setq val (list 'error
  727.                 (+ (aref info 2) pref-len (nth 1 val))
  728.                 (nth 2 val))))
  729.         (aset info 6 str)
  730.         (aset info 8 val)
  731.         (setq temp val)
  732.         (if (eq (car-safe temp) 'calcFunc-evalto)
  733.         (setq temp (nth 1 temp))
  734.           (if (eq (car-safe temp) 'error)
  735.           (if new-info
  736.               (setq new-info nil)
  737.             (setcdr found (delq info (cdr found)))
  738.             (calc-embedded-active-state 'less))))
  739.         (aset info 9 (and (eq (car-safe temp) 'calcFunc-assign)
  740.                   (nth 1 temp)))
  741.         (if (memq (car-safe val) '(calcFunc-evalto calcFunc-assign))
  742.         (calc-embedded-find-vars val))
  743.         (aset info 10 vars-used)
  744.         (aset info 11 nil))))
  745.     (if new-info
  746.     (progn
  747.       (setcdr found (cons info (cdr found)))
  748.       (calc-embedded-active-state 'more)))
  749.     info)
  750. (defun calc-embedded-find-vars (x)
  751.   (cond ((Math-primp x)
  752.      (and (eq (car-safe x) 'var)
  753.           (not (assoc x vars-used))
  754.           (setq vars-used (cons (list x) vars-used))))
  755.     ((eq (car x) 'calcFunc-evalto)
  756.      (calc-embedded-find-vars (nth 1 x)))
  757.     ((eq (car x) 'calcFunc-assign)
  758.      (calc-embedded-find-vars (nth 2 x)))
  759.      (and (eq (car x) 'calcFunc-subscr)
  760.           (eq (car-safe (nth 1 x)) 'var)
  761.           (Math-primp (nth 2 x))
  762.           (not (assoc x vars-used))
  763.           (setq vars-used (cons (list x) vars-used)))
  764.      (while (setq x (cdr x))
  765.        (calc-embedded-find-vars (car x)))))
  766. (defun calc-embedded-evaluate-expr (x)
  767.   (let ((vars-used (aref calc-embedded-info 10)))
  768.     (or vars-used (calc-embedded-find-vars x))
  769.     (if vars-used
  770.     (let ((active (assq (aref calc-embedded-info 0) calc-embedded-active))
  771.           (args nil))
  772.       (save-excursion
  773.         (calc-embedded-original-buffer t)
  774.         (or active
  775.         (progn
  776.           (calc-embedded-activate)
  777.           (setq active (assq (aref calc-embedded-info 0)
  778.                      calc-embedded-active))))
  779.         (while vars-used
  780.           (calc-embedded-eval-get-var (car (car vars-used)) active)
  781.           (setq vars-used (cdr vars-used))))
  782.       (calc-embedded-subst x))
  783.       (calc-normalize (math-evaluate-expr-rec x))))
  784. (defun calc-embedded-subst (x)
  785.   (if (and (eq (car-safe x) 'calcFunc-evalto) (cdr x))
  786.       (let ((rhs (calc-embedded-subst (nth 1 x))))
  787.     (list 'calcFunc-evalto
  788.           (nth 1 x)
  789.           (if (eq (car-safe rhs) 'calcFunc-assign) (nth 2 rhs) rhs)))
  790.     (if (and (eq (car-safe x) 'calcFunc-assign) (= (length x) 3))
  791.     (list 'calcFunc-assign
  792.           (nth 1 x)
  793.           (calc-embedded-subst (nth 2 x)))
  794.       (calc-normalize (math-evaluate-expr-rec (math-multi-subst-rec x)))))
  795. (defun calc-embedded-eval-get-var (var base)
  796.   (let ((entry base)
  797.     (point (aref calc-embedded-info 2))
  798.     (last nil)
  799.     val)
  800.     (while (and (setq entry (cdr entry))
  801.         (or (not (equal var (aref (car entry) 9)))
  802.             (and (> point (aref (car entry) 3))
  803.              (setq last entry)))))
  804.     (if last
  805.     (setq entry last))
  806.     (if entry
  807.     (progn
  808.       (setq entry (car entry))
  809.       (if (equal (buffer-substring (aref entry 2) (aref entry 3))
  810.              (aref entry 6))
  811.           (progn
  812.         (or (aref entry 11)
  813.             (save-excursion
  814.               (calc-embedded-update entry 14 t nil)))
  815.         (setq val (aref entry 11))
  816.         (if (eq (car-safe val) 'calcFunc-evalto)
  817.             (setq val (nth 2 val)))
  818.         (if (eq (car-safe val) 'calcFunc-assign)
  819.             (setq val (nth 2 val)))
  820.         (setq args (cons (cons var val) args)))
  821.         (calc-embedded-activate)
  822.         (calc-embedded-eval-get-var var base)))))
  823. (defun calc-embedded-update (info which need-eval need-display
  824.                   &optional str entry old-val)
  825.   (let* ((prev-modes nil)
  826.      (open-plain calc-embedded-open-plain)
  827.      (close-plain calc-embedded-close-plain)
  828.      (vars-used nil)
  829.      (evalled nil)
  830.      (val (aref info 8))
  831.      (old-eval (aref info 11)))
  832.     (or old-val (setq old-val val))
  833.     (if (eq (car-safe val) 'calcFunc-evalto)
  834.     (setq need-display t))
  835.     (unwind-protect
  836.     (progn
  837.       (set-buffer (aref info 1))
  838.       (and which
  839.            (calc-embedded-set-modes (aref info 15) (aref info 12)
  840.                     (aref info which)
  841.                     (if need-display 'full t)))
  842.       (if (memq (car-safe val) '(calcFunc-evalto calcFunc-assign))
  843.           (calc-embedded-find-vars val))
  844.       (if need-eval
  845.           (let ((calc-embedded-info info))
  846.         (setq val (math-evaluate-expr val)
  847.               evalled val)))
  848.       (if (or (eq need-eval 'eval) (eq (car-safe val) 'calcFunc-evalto))
  849.           (aset info 8 val))
  850.       (aset info 9 nil)
  851.       (aset info 10 vars-used)
  852.       (aset info 11 nil)
  853.       (if (or need-display (eq (car-safe val) 'calcFunc-evalto))
  854.           (let ((extra (if (eq calc-language 'big) 1 0)))
  855.         (or entry (setq entry (list val 1 nil)))
  856.         (or str (progn
  857.               (setq str (let ((calc-line-numbering nil))
  858.                       (math-format-stack-value entry)))
  859.               (if (eq calc-language 'big)
  860.                   (setq str (substring str 0 -1)))))
  861.         (and calc-show-plain
  862.              (setq str (concat open-plain
  863.                        (math-showing-full-precision
  864.                     (math-format-flat-expr val 0))
  865.                        close-plain
  866.                        str)))
  867.         (save-excursion
  868.           (calc-embedded-original-buffer t info)
  869.           (or (equal str (aref info 6))
  870.               (let ((delta (- (aref info 5) (aref info 3)))
  871.                 (buffer-read-only nil))
  872.             (goto-char (aref info 2))
  873.             (delete-region (point) (aref info 3))
  874.             (and (> (nth 1 entry) (1+ extra))
  875.                  (aref info 7)
  876.                  (progn
  877.                    (aset info 7 nil)
  878.                    (delete-horizontal-space)
  879.                    (insert "\n\n")
  880.                    (delete-horizontal-space)
  881.                    (backward-char 1)))
  882.             (insert str)
  883.             (set-marker (aref info 3) (point))
  884.             (set-marker (aref info 5) (+ (point) delta))
  885.             (aset info 6 str))))))
  886.       (if (eq (car-safe val) 'calcFunc-evalto)
  887.           (progn
  888.         (setq evalled (nth 2 val)
  889.               val (nth 1 val))))
  890.       (if (eq (car-safe val) 'calcFunc-assign)
  891.           (progn
  892.         (aset info 9 (nth 1 val))
  893.         (aset info 11 (or evalled
  894.                   (let ((calc-embedded-info info))
  895.                     (math-evaluate-expr (nth 2 val)))))
  896.         (or (equal old-eval (aref info 11))
  897.             (calc-embedded-var-change (nth 1 val) (aref info 0))))
  898.         (if (eq (car-safe old-val) 'calcFunc-evalto)
  899.         (setq old-val (nth 1 old-val)))
  900.         (if (eq (car-safe old-val) 'calcFunc-assign)
  901.         (calc-embedded-var-change (nth 1 old-val) (aref info 0)))))
  902.       (set-buffer (aref info 1))
  903.       (while prev-modes
  904.     (cond ((eq (car (car prev-modes)) 'the-language)
  905.            (if need-display
  906.            (calc-embedded-set-language (cdr (car prev-modes)))))
  907.           ((eq (car (car prev-modes)) 'the-display-just)
  908.            (if need-display
  909.            (calc-embedded-set-justify (cdr (car prev-modes)))))
  910.           (t
  911.            (set (car (car prev-modes)) (cdr (car prev-modes)))))
  912.     (setq prev-modes (cdr prev-modes)))))
  913. ;;; These are hooks called by the main part of Calc.
  914. (defun calc-embedded-select-buffer ()
  915.   (if (eq (current-buffer) (aref calc-embedded-info 0))
  916.       (let ((info calc-embedded-info)
  917.         horiz vert)
  918.     (if (and (or (< (point) (aref info 4))
  919.              (> (point) (aref info 5)))
  920.          (not calc-embedded-no-reselect))
  921.         (let ((calc-embedded-quiet t))
  922.           (message "(Switching Calc Embedded mode to new formula.)")
  923.           (calc-embedded nil)
  924.           (calc-embedded nil)))
  925.     (setq horiz (max (min (current-column) (- (point) (aref info 2))) 0)
  926.           vert (if (<= (aref info 2) (point))
  927.                (- (count-lines (aref info 2) (point))
  928.               (if (bolp) 0 1))
  929.              0))
  930.     (set-buffer (aref info 1))
  931.     (if calc-show-plain
  932.         (if (= vert 0)
  933.         (setq horiz 0)
  934.           (setq vert (1- vert))))
  935.     (calc-cursor-stack-index 1)
  936.     (if calc-line-numbering
  937.         (setq horiz (+ horiz 4)))
  938.     (if (> vert 0)
  939.         (forward-line vert))
  940.     (forward-char (min horiz
  941.                (- (point-max) (point)))))
  942.     (calc-select-buffer))
  943. (setq calc-embedded-no-reselect nil)
  944. (defun calc-embedded-finish-command ()
  945.   (let ((buf (current-buffer))
  946.     horiz vert)
  947.     (save-excursion
  948.       (set-buffer (aref calc-embedded-info 1))
  949.       (if (> (calc-stack-size) 0)
  950.       (let ((pt (point))
  951.         (col (current-column))
  952.         (bol (bolp)))
  953.         (calc-cursor-stack-index 0)
  954.         (if (< pt (point))
  955.         (progn
  956.           (calc-cursor-stack-index 1)
  957.           (if (>= pt (point))
  958.               (progn
  959.             (setq horiz (- col (if calc-line-numbering 4 0))
  960.                   vert (- (count-lines (point) pt)
  961.                       (if bol 0 1)))
  962.             (if calc-show-plain
  963.                 (setq vert (max 1 (1+ vert))))))))
  964.         (goto-char pt))))
  965.     (if horiz
  966.     (progn
  967.       (set-buffer (aref calc-embedded-info 0))
  968.       (goto-char (aref calc-embedded-info 2))
  969.       (if (> vert 0)
  970.           (forward-line vert))
  971.       (forward-char (max horiz 0))
  972.       (set-buffer buf))))
  973. (defun calc-embedded-stack-change ()
  974.   (or calc-executing-macro
  975.       (save-excursion
  976.     (set-buffer (aref calc-embedded-info 1))
  977.     (let* ((info calc-embedded-info)
  978.            (extra-line (if (eq calc-language 'big) 1 0))
  979.            (the-point (point))
  980.            (empty (= (calc-stack-size) 0))
  981.            (entry (if empty
  982.               (list '(var empty var-empty) 1 nil)
  983.             (calc-top 1 'entry)))
  984.            (old-val (aref info 8))
  985.            top bot str)
  986.       (if empty
  987.           (setq str "empty")
  988.         (save-excursion
  989.           (calc-cursor-stack-index 1)
  990.           (setq top (point))
  991.           (calc-cursor-stack-index 0)
  992.           (setq bot (- (point) extra-line))
  993.           (setq str (buffer-substring top (- bot 1))))
  994.         (if calc-line-numbering
  995.         (let ((pos 0))
  996.           (setq str (substring str 4))
  997.           (while (setq pos (string-match "\n...." str pos))
  998.             (setq str (concat (substring str 0 (1+ pos))
  999.                       (substring str (+ pos 5)))
  1000.               pos (1+ pos))))))
  1001.       (calc-embedded-original-buffer t)
  1002.       (aset info 8 (car entry))
  1003.       (calc-embedded-update info 13 nil t str entry old-val))))
  1004. (defun calc-embedded-mode-line-change ()
  1005.   (let ((str mode-line-buffer-identification))
  1006.     (save-excursion
  1007.       (calc-embedded-original-buffer t)
  1008.       (setq mode-line-buffer-identification str)
  1009.       (set-buffer-modified-p (buffer-modified-p))))
  1010. (defun calc-embedded-modes-change (vars)
  1011.   (if (eq (car vars) 'calc-language) (setq vars '(the-language)))
  1012.   (if (eq (car vars) 'calc-display-just) (setq vars '(the-display-just)))
  1013.   (while (and vars
  1014.           (not (rassq (car vars) calc-embedded-mode-vars)))
  1015.     (setq vars (cdr vars)))
  1016.   (if (and vars calc-mode-save-mode (not (eq calc-mode-save-mode 'save)))
  1017.       (save-excursion
  1018.     (let* ((save-mode calc-mode-save-mode)
  1019.            (header (if (eq save-mode 'local)
  1020.                "calc-mode:"
  1021.              (format "calc-%s-mode:" save-mode)))
  1022.            (the-language (calc-embedded-language))
  1023.            (the-display-just (calc-embedded-justify))
  1024.            (values (mapcar 'symbol-value vars))
  1025.            (num (cond ((eq save-mode 'local) 12)
  1026.               ((eq save-mode 'edit) 13)
  1027.               ((eq save-mode 'perm) 14)
  1028.               (t nil)))
  1029.            base limit mname mlist)
  1030.       (calc-embedded-original-buffer t)
  1031.       (save-excursion
  1032.         (if (eq save-mode 'global)
  1033.         (setq base (point-max)
  1034.               limit (point-min)
  1035.               mlist calc-embedded-globals)
  1036.           (goto-char (aref calc-embedded-info 4))
  1037.           (beginning-of-line)
  1038.           (setq base (point)
  1039.             limit (max (- (point) 1000) (point-min))
  1040.             mlist (and num (aref calc-embedded-info num)))
  1041.           (and (re-search-backward
  1042.             (format "\\(%s\\)[^\001]*\\(%s\\)\\|\\[calc-defaults]"
  1043.                 calc-embedded-open-formula
  1044.                 calc-embedded-close-formula) limit t)
  1045.            (setq limit (point))))
  1046.         (while vars
  1047.           (goto-char base)
  1048.           (if (setq mname (car (rassq (car vars)
  1049.                       calc-embedded-mode-vars)))
  1050.           (let ((buffer-read-only nil)
  1051.             (found (assq (car vars) mlist)))
  1052.             (if found
  1053.             (setcdr found (car values))
  1054.               (setq mlist (cons (cons (car vars) (car values)) mlist))
  1055.               (if num
  1056.               (aset calc-embedded-info num mlist)
  1057.             (if (eq save-mode 'global)
  1058.                 (setq calc-embedded-globals mlist))))
  1059.             (if (re-search-backward
  1060.              (format "\\[%s *%s: *\\(\"\\([^\"\n\\]\\|\\\\.\\)*\"\\|[- ()a-zA-Z0-9]+\\)]"
  1061.                  header mname)
  1062.              limit t)
  1063.             (progn
  1064.               (goto-char (match-beginning 1))
  1065.               (delete-region (point) (match-end 1))
  1066.               (insert (prin1-to-string (car values))))
  1067.               (goto-char base)
  1068.               (insert-before-markers
  1069.                calc-embedded-open-mode
  1070.                "[" header " " mname ": "
  1071.                (prin1-to-string (car values)) "]"
  1072.                calc-embedded-close-mode))))
  1073.           (setq vars (cdr vars)
  1074.             values (cdr values)))))))
  1075. (defun calc-embedded-var-change (var &optional buf)
  1076.   (if (symbolp var)
  1077.       (setq var (list 'var
  1078.               (if (string-match "\\`var-.+\\'"
  1079.                     (symbol-name var))
  1080.               (intern (substring (symbol-name var) 4))
  1081.             var)
  1082.               var)))
  1083.   (save-excursion
  1084.     (let ((manual (not calc-auto-recompute))
  1085.       (bp calc-embedded-active)
  1086.       (first t))
  1087.       (if buf (setq bp (memq (assq buf bp) bp)))
  1088.       (while bp
  1089.     (let ((calc-embedded-no-reselect t)
  1090.           (p (and (buffer-name (car (car bp)))
  1091.               (cdr (car bp)))))
  1092.       (while p
  1093.         (if (assoc var (aref (car p) 10))
  1094.         (if manual
  1095.             (if (aref (car p) 11)
  1096.             (progn
  1097.               (aset (car p) 11 nil)
  1098.               (if (aref (car p) 9)
  1099.                   (calc-embedded-var-change (aref (car p) 9)))))
  1100.           (set-buffer (aref (car p) 0))
  1101.           (if (equal (buffer-substring (aref (car p) 2)
  1102.                            (aref (car p) 3))
  1103.                  (aref (car p) 6))
  1104.               (let ((calc-embedded-info nil))
  1105.             (or calc-embedded-quiet
  1106.                 (message "Recomputing..."))
  1107.             (setq first nil)
  1108.             (calc-wrapper
  1109.              (set-buffer (aref (car p) 0))
  1110.              (calc-embedded-update (car p) 14 t nil)))
  1111.             (setcdr (car bp) (delq (car p) (cdr (car bp))))
  1112.             (message
  1113.              "(Tried to recompute but formula was changed or missing.)"))))
  1114.         (setq p (cdr p))))
  1115.     (setq bp (if buf nil (cdr bp))))
  1116.       (or first calc-embedded-quiet (message ""))))
  1117.